package com.algo.panelmn;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.reduce.LongSumReducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class SnsReduce extends Configured implements Tool {
	public static class Mapper extends org.apache.hadoop.mapreduce.Mapper<Text, Text, Text, LongWritable>{
		public void map(Text key, Text value, Context context)
				throws IOException, InterruptedException {
			long freq = Long.parseLong(value.toString());
			context.write(key, new LongWritable(freq));
		}
	}
	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new SnsReduce(), args);
		System.exit(res);
	}
	public int run(String[] args) throws Exception {
		for(int i = 0; i< args.length; i++)
			System.out.println("step2:" + args[i]);
		Configuration conf = getConf();
		Job job = new Job(conf, "SnsReducer");
		job.setJarByClass(SnsReduce.class);
		
		job.setMapperClass(Mapper.class);
		job.setReducerClass(LongSumReducer.class);
		
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.setNumReduceTasks(3);	
		return job.waitForCompletion(true) ? 0 : 1;
	}
}
